package com.yxhl.zoume.core.user.ui.fragment.passenger;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yxhl.zoume.R;
import com.yxhl.zoume.account.event.UserAbortLoginEvent;
import com.yxhl.zoume.account.event.UserLoginEvent;
import com.yxhl.zoume.common.adapter.UpPullLoadDataAdapter;
import com.yxhl.zoume.common.presenter.UpPullPresenter;
import com.yxhl.zoume.common.ui.event.AddPassengerEvent;
import com.yxhl.zoume.common.ui.event.UpdatePassengerEvent;
import com.yxhl.zoume.common.ui.fragment.UpPullBaseFragment;
import com.yxhl.zoume.core.user.presenter.passenger.PassengerMgmtPresenter;
import com.yxhl.zoume.core.user.ui.activity.UserContainerActivity;
import com.yxhl.zoume.core.user.ui.adapter.PassengerListAdapter;
import com.yxhl.zoume.core.user.view.passenger.PassengerMgmtView;
import com.yxhl.zoume.data.http.model.passenger.ZMPassenger;
import com.yxhl.zoume.di.component.usercenter.PassengerComponent;
import com.yxhl.zoume.utils.LOG;

import java.util.List;

import javax.inject.Inject;

import butterknife.OnClick;
import rx.functions.Action1;

/**
 * created by sugarya   Email:sugarya@foxmail.com
 * 乘客管理界面
 */
public class PassengerMgmtFragment extends UpPullBaseFragment<ZMPassenger> implements PassengerMgmtView {

    private static final String TAG = "PassengerMgmtFrag";


    //arguments
    @Inject
    public PassengerMgmtPresenter mPassengerManagerPresenter;

    public PassengerMgmtFragment() {
        // Required empty public constructor
    }

    public static PassengerMgmtFragment newInstance() {
        PassengerMgmtFragment fragment = new PassengerMgmtFragment();
        return fragment;
    }

    public void onRxBusEvent() {
        getSubscription().add(toSubscription(UserAbortLoginEvent.class,
                new Action1<UserAbortLoginEvent>() {
                    @Override
                    public void call(UserAbortLoginEvent userAbortLoginEvent) {
                        getActivity().finish();
                    }
                },
                new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LOG.e(TAG, "throwable=" + throwable.toString());
                        onRxBusEvent();
                    }
                }
        ));

        getSubscription().add(toSubscription(AddPassengerEvent.class,
                new Action1<AddPassengerEvent>() {
                    @Override
                    public void call(AddPassengerEvent addPassengerEvent) {
                        if (addPassengerEvent != null) {
                            if (mDataList != null) {
                                ZMPassenger zmPassenger = addPassengerEvent.getZmPassenger();
                                if (zmPassenger != null) {
                                    mDataList.add(0, zmPassenger);
                                    if (mUpPullLoadDataAdapter != null) {
                                        mUpPullLoadDataAdapter.notifyItemInserted(0);
                                    }
                                    if (mRecyclerView != null) {
                                        mRecyclerView.scrollToPosition(0);
                                    }
                                }
                            }
                        }
                    }
                },
                new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LOG.e(TAG, "throwable=" + throwable.toString());
                        onRxBusEvent();
                    }
                }
        ));

        getSubscription().add(toSubscription(UpdatePassengerEvent.class,
                new Action1<UpdatePassengerEvent>() {
                    @Override
                    public void call(UpdatePassengerEvent updatePassengerEvent) {
                        ZMPassenger eventPassenger = updatePassengerEvent.getZmPassenger();
                        if (mDataList.contains(eventPassenger)) {
                            LOG.e(TAG, "call UpdatePassengerEvent");

                            int indexOf = mDataList.indexOf(eventPassenger);
                            ZMPassenger passenger = mDataList.get(indexOf);

                            String idNumber = eventPassenger.getIdNumber();
                            passenger.setIdNumber(idNumber);

                            String mobileNumber = eventPassenger.getMobileNumber();
                            passenger.setMobileNumber(mobileNumber);

                            int psgerCategory = eventPassenger.getPsgerCategory();
                            passenger.setPsgerCategory(psgerCategory);

                            mUpPullLoadDataAdapter.notifyDataSetChanged();
                        }
                    }
                },
                new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LOG.e(TAG, "throwable=" + throwable.toString());
                        onRxBusEvent();
                    }
                }
        ));

        getSubscription().add(toSubscription(UserLoginEvent.class,
                new Action1<UserLoginEvent>() {
                    @Override
                    public void call(UserLoginEvent userLoginEvent) {
                        if (mPassengerManagerPresenter != null) {
                            mPassengerManagerPresenter.loadDownRefreshData();
                        }
                    }
                },
                new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LOG.e(TAG, "throwable=" + throwable.toString());
                        onRxBusEvent();
                    }
                }
        ));
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LOG.e(TAG, "onCreate");
        onRxBusEvent();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        LOG.e(TAG, "onCreateView savedInstanceState=" + savedInstanceState);
        View view = super.onCreateView(inflater, container, savedInstanceState);
        mPassengerManagerPresenter.attachView(this);

        mPassengerManagerPresenter.loadDownRefreshData();

        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            setupToolBar();
        }
    }

    @OnClick(R.id.reveal_fragment_passenger_add_layout)
    public void onAddPassengerClick(View view) {
        if (view != null) {
            getAppComponent().navigator().navigateToAddPassengerActivity(mActivity);
        }
    }

    @Override
    public void renderDataOnSuccess(List<ZMPassenger> passengerList) {
        if (passengerList != null && passengerList.size() > 0) {
            mDataList.clear();
            mDataList.addAll(passengerList);
            LOG.e(TAG, "passengerList=" + passengerList.toString());
            mUpPullLoadDataAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void closeRefreshAnimation() {
        if (mSwipeRefreshLayout != null) {
            if (mSwipeRefreshLayout.isRefreshing()) {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }
    }

    @NonNull
    @Override
    protected UpPullLoadDataAdapter getUpPullLoadDataAdapter(LinearLayoutManager layoutManager) {
        return new PassengerListAdapter(mActivity, layoutManager, mDataList, mPassengerManagerPresenter);
    }

    @Override
    protected UpPullPresenter getUpPullPresenter() {
        return mPassengerManagerPresenter;
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_passenger_manager;
    }

    @Override
    protected void setupToolBar() {
        ((UserContainerActivity) mActivity).setToolbarTitle(getString(R.string.passengers_management));
    }

    @Override
    protected void initializeInjector() {
        getComponent(PassengerComponent.class).inject(this);
    }


}
